const config = require('./config')
const util = require('./utils/util')
App({
    onLaunch: function () {
        this.getUserOpenId()
    },
    
    // 获取用户openid 
    getUserOpenId: function(callback) {
        var self = this
        wx.showLoading({
            title:'加载中',
            mask:true
        })
        if (!self.globalData.openid) {
        wx.login({
            success: function(data) {
                var headerkey = util.createheaderkey()
                wx.request({
                    url: config.host+'/api/user/getUserId',
                    method: 'GET',
                    header: {
                        'content-type': 'application/json',
                        'key': headerkey.key,
                        'time': headerkey.timestamp
                    },
                    data: {
                        code: data.code
                    },
                    success: function(res) {
                        
                        if (res.data.code==200){
                            //console.log(res.data)
                            self.globalData.uid = res.data.data.uid
                            
                            if (res.data.data.userData.name){
                                wx.hideLoading()
                                self.globalData.nickName = res.data.data.userData.name
                                self.globalData.avatarUrl = res.data.data.userData.picture
                            }else{
                                self.getuserinfo()
                            }
                        }else{
                            wx.hideLoading()
                            wx.showToast({
                                title: '1用户信息获取失败，请联系技术支持！',
                                duration: 3000
                            })
                        }
                    },
                    fail: function(res) {
                        wx.hideLoading()
                        wx.showToast({
                            title: '2用户信息请求失败，请联系技术支持！',
                            duration: 3000
                        })
                    }
                })
            },
            fail: function (err) {
                wx.hideLoading()
                wx.showToast({
                    title: '基础接口调用失败！请联系技术支持！',
                    duration: 3000
                })
            }
        })
        }
    },
    getuserinfo: function () {
        var that = this
        wx.getUserInfo({
            success: function (res) {
                var userInfo = res.userInfo,
                    headerkey = util.createheaderkey()
                wx.request({
                    url: config.host + '/api/user/saveUserInfo',
                    method: 'POST',
                    header: {
                        'content-type': 'application/json',
                        'key': headerkey.key,
                        'time': headerkey.timestamp
                    },
                    data: {
                        userId: that.globalData.uid,
                        userInfo: userInfo,
                    },
                    success: function (res) {
                        wx.hideLoading()
                        if (res.data.code == 200) {
                            that.globalData.nickName = userInfo.nickName
                            that.globalData.avatarUrl = userInfo.avatarUrl
                            return userInfo;
                        }else{
                            wx.showModal({
                                title: '保存用户信息失败',
                                content: res.data.msg ,
                                showCancel:false
                            })
                        }

                    },
                    fail: function (res) {
                        wx.hideLoading()
                        wx.showToast({
                            title: '保存用户信息失败!',
                            duration: 3000
                        })
                    }
                })
            },
            fail: function (res) {
                wx.hideLoading()
                wx.showModal({
                    title: '提示',
                    content: '给朋友发送祝福时需要获取你的昵称和头像信息朋友才能知道你是谁哦，点击确定授权头像信息，或者点击取消继续当神秘用户',
                    success: function (res) {
                        if (res.confirm) {
                            wx.openSetting({
                                success: (res) => {
                                    wx.authorize({
                                        scope: 'scope.userInfo',
                                        success(info) {
                                            wx.showLoading({
                                                title: '加载中',
                                                mask: true
                                            })
                                            that.getuserinfo()
                                        },

                                    })
                                }
                            })
                        } else if (res.cancel) {
                            that.globalData.nickName = '神秘用户'
                            //console.log('用户点击取消')
                        }
                    }
                })
            }
        })


    },
    getheaderkey: function(){
        var time = Date.now(),
            key = headerkey

    },
    saveformid: function (formid){
        var that = this,
            headerkey = util.createheaderkey()
        wx.request({
            url: config.host + '/api/form/saveform',
            method: 'GET',
            header: {
                'content-type': 'application/json',
                'key': headerkey.key,
                'time': headerkey.timestamp
            },
            data: {
                uid: that.globalData.uid,
                form_id: formid,
            },
            success: function (res) {
                
                if (res.data.code == 200) {
                   
                } else {
                    wx.showModal({
                        title: '保存formid失败',
                        content: res.data.msg,
                        showCancel: false
                    })
                }

            },
            fail: function (res) {
                wx.showToast({
                    title: '保存formid失败!',
                    duration: 3000
                })
            }
        })
    },
    globalData: {
        uid:null,
        nickName:null,
        avatarUrl: null
    },
})